import { cloud } from '@hw-agconnect/cloud-server';
import { Book } from './book'; // BookInfo是Server Model

// default是BookInfo对象类型使用的存储区名
const collection = cloud.database({ zoneName: 'default' }).collection(Book);

interface IUpsertRes {
  result: number
}

export class BookObj {
  method1(param1, param2) {
    console.info("this is message of info log");

    // do something here

    return { "simple": "example" };
  }

  query() {
    return collection.query().get();
  }

  // 主键有的话就执行更新操作，主键不存在执行插入操作
  upset(bookInfos: Book[]) {
    return new Promise<IUpsertRes>((resolve, reject) => {
      collection.upsert(bookInfos.map(item => Book.parseFrom(item)))
        .then(result => resolve({ result }))
        .catch(err => reject(err))
    });
  }
}
